
// Copyright (c) 2002 HTHK
package admin.menu;

import admin.*;
import admin.system.user.User;

import java.sql.*;
import java.util.*;
import java.text.ParseException;

import com.ffpc.sql.*;
import com.ffpc.util.*;

/**
 * A Class class.
 * <P>
 * @author HTHK
 */
public class MenuFactory
{
  private User user;
  private Folder root;
  /**
   * Constructor
   */
  public MenuFactory()
  {
      this.user=null;
      this.root = null;
  }

  public void setUser(User user)
  {
      this.user = user;
  }

  public User getUser()
  {
      return this.user;
  }

  public void load(Connection con)
    throws DataException
  {
      if (con==null)
          throw new DataException("Connection is null.");
      if (this.getUser()==null)
          throw new DataException("Login user is null, can't retrieve menu.");
      UserFunctionViewProcess ufvProcess = new UserFunctionViewProcess();
      UserFunctionViewBean bean =  UserFunctionViewProcess.createBean();
      bean.setUser_id(this.getUser().getUser_id());
      UserFunctionViewBean[] functions = UserFunctionViewProcess.find(con,bean);
      try
      {
          createRoot(functions);
      }
      catch (ParseException ex)
      {
          throw new DataException(ex.getMessage());
      }
  }

  public Folder getRoot()
  {
      return this.root;
  }

  public String getTreeMenu()
  {

      StringBuffer menu = new StringBuffer();
      this.createTreeStructure(this.getRoot(),menu);
      return menu.toString();
  }

  private void createTreeStructure(Folder parent,StringBuffer menu)
  {
      menu =  menu.append("\n var ")
              .append(parent.getId())
              .append(" = null; \n ")
              .append(parent.getId())
              .append(" = new MTMenu();\n");
      Vector subFolders = sort(parent.getFolders());
      Vector subItems = sort(parent.getItems());
      for (int i=0;i<subFolders.size();i++)
      {
          Folder child = (Folder)subFolders.get(i);
          menu = menu.append(parent.getId())
                     .append(".addItem(\"")
                     .append(child.getName())
                     .append("\");\n");
          createTreeStructure(child,menu);
          menu = menu.append(parent.getId())
                     .append(".makeLastSubmenu(")
                     .append(child.getId())
                     .append(");\n");
      }
      for (int i=0;i<subItems.size();i++)
      {
          Item child = (Item)subItems.get(i);
          menu = menu.append(parent.getId())
                     .append(".addItem(\"")
                     .append(child.getName())
                     .append("\", \"")
                     .append(child.getUrl())
                     .append("\");\n");
      }
  }

  private Vector sort(Vector items)
  {
      Vector sortedItems = new Vector();
      Hashtable hashItems = new Hashtable();
      int[] sequence = new int[items.size()];
      for (int i=0;i<items.size();i++)
      {
          sequence[i] = ((Item)items.get(i)).getSequence();
          hashItems.put(new Integer(sequence[i]),items.get(i));
      }
      Arrays.sort(sequence);
      for (int i=0;i<sequence.length;i++)
      {
          sortedItems.add(i,hashItems.get(new Integer(sequence[i])));
      }
      return sortedItems;
  }

  private void createRoot(UserFunctionViewBean[] functions)
      throws ParseException
  {
      Hashtable hFolders = new Hashtable();
      Hashtable hItems = new Hashtable();
      for (int i=0; i<functions.length; i++)
      {
          if (functions[i].getParent_fg().equalsIgnoreCase("Y"))
              hFolders.put( functions[i].getFunc_id(),
                             new Folder(  functions[i].getFunc_id(),
                                          functions[i].getFunc_name(),
                                          functions[i].getUrl(),
                                          NumberFormatter.parseInt(functions[i].getSequence()),
                                          functions[i].getParent_id()
                                          )
                          );
          else
              hItems.put( functions[i].getFunc_id(),
                             new Item(    functions[i].getFunc_id(),
                                          functions[i].getFunc_name(),
                                          functions[i].getUrl(),
                                          NumberFormatter.parseInt(functions[i].getSequence()),
                                          functions[i].getParent_id()
                                          )
                        );
      }
      for (Enumeration e =hFolders.elements(); e.hasMoreElements();)
      {
          Folder folder = (Folder)e.nextElement();
          if (hFolders.containsKey(folder.getParentId()))
          {
              if (folder.getParentId()!=null && folder.getParentId().trim().length()>0)
              {
                  Folder parent = (Folder)hFolders.get(folder.getParentId());
                  parent.addFolder(folder);
              }
          }
      }
      for (Enumeration e =hItems.elements(); e.hasMoreElements();)
      {
          Item item = (Item)e.nextElement();
          if (hFolders.containsKey(item.getParentId()))
          {
              Folder parent = (Folder)hFolders.get(item.getParentId());
              parent.addItem(item);
          }
      }
      for (Enumeration e =hFolders.elements(); e.hasMoreElements();)
      {
          Folder folder = (Folder)e.nextElement();
          if (folder.isRoot())
          {
              this.root = folder;
          }
      }
  }
}

 